package com.tensquare.article.controller;

import com.tensquare.article.pojo.Comment;
import com.tensquare.article.service.CommentService;
import entity.Result;
import entity.StatusCode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;
import util.IdWorker;

import java.util.List;

@RestController
@RequestMapping("/comment")
@CrossOrigin
public class CommentController {

    @Autowired
    private CommentService commentService;
    @Autowired
    private RedisTemplate redisTemplate;
    //新增
    @PostMapping
    public Result save(@RequestBody Comment comment){
        commentService.save(comment);
        return new Result(true, StatusCode.OK,"ok");
    }
    //删除-id
    @DeleteMapping("{id}")
    public Result deleteById(@PathVariable String id){
        commentService.deleteById(id);
        return new Result(true,StatusCode.OK,"ok");
    }
    //修改-条件-id
    @PutMapping("{id}")
    public Result updateByid(@PathVariable String id, @RequestBody Comment comment){
        comment.set_id(id);
        commentService.updateByid(comment);
        return new Result(true,StatusCode.OK,"ok");
    }
    //查询全部
    @GetMapping
    public Result findAll(){
        List<Comment>list=commentService.findAll();
        return new Result(true,StatusCode.OK,"ok", list);
        }
    //查询-id
    @GetMapping("{id}")
    public Result findById(@PathVariable String id){
        Comment comment=commentService.findById(id);
        return new Result(true,StatusCode.OK,"ok",comment);
    }
    //查询-id
    @GetMapping("/article/{articleId}")
    public Result findByAId(@PathVariable String articleId){
        List<Comment> list =commentService.findByAId(articleId);
        return new Result(true,StatusCode.OK,"ok",list);
    }
    //评论点赞-id
    @PutMapping("/thumbup/{id}")
    public Result thumbup(@PathVariable String id){
        String userid="123";
        Object res=redisTemplate.opsForValue().get("thumbup_"+userid+"_"+id);
        if(res!=null){
            return  new Result(false,StatusCode.REPERROR,"no");
        }
        commentService.thumbup(id);
        redisTemplate.opsForValue().set("thumbup_"+userid+"_"+id,1);
        return new Result(true,StatusCode.OK,"ok");
    }
}
